/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webanovare.ruzzle.rest;

import com.webanovare.ruzzle.mgr.GameManager;
import com.webanovare.ruzzle.mgr.impl.IGame;
import com.webanovare.ruzzle.misc.Player;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;

@Path("/ruzzle")
@XmlRootElement(name = "Ruzzle")
public class GameResource {
    
    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Path("{gid}/submitWord")
    public void addWord(@PathParam("gid") int gameId, @FormParam("list") String wordPoints, @FormParam("player") String player) {
        List<Point> points = new ArrayList<Point>();
        char[] letters = wordPoints.toCharArray();
        System.out.println(letters);
        for (int i = 0; i < letters.length; i = i+2) {
            points.add(new Point(Integer.parseInt(letters[i] + ""), Integer.parseInt(letters[i+1] + "")));
        } 
        GameManager.getInstance().getGame(gameId).addWord(points, new Player(player));
    }
    
    @GET
    @Produces({MediaType.TEXT_PLAIN})
    @Path("/{gid}/{player}")
    public String getPoints(@PathParam("gid") int gid, @PathParam("player") String player) {
        Player p = new Player(player);
        GameManager gm = GameManager.getInstance();
        IGame g = gm.getGame(gid);
        int points = g.getPoints(p);
        return points + "";
    }
}
